package com.itheima.controller.user;

import com.itheima.common.result.Result;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController()
@RequestMapping("common")
public class CommonController {
    @PostMapping("upload")
    public Result upload(MultipartFile file,String type) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        String str = UUID.randomUUID()+"."+split[1];

        FileOutputStream fos = new FileOutputStream("D:\\czbk\\develop\\nginx-front\\html\\dist\\apiupload\\images\\20181018\\"+str);

        InputStream inputStream = file.getInputStream();
        byte[] b =new byte[1024];
        int len;
        while((len = inputStream.read(b))!=-1){
            fos.write(b,0,len);
        };
        fos.flush();
        fos.close();
        inputStream.close();
        Map map = new HashMap();
        map.put("savePath","upload/images/20181018/"+str);
        map.put("mime","image/png");
        map.put("name",file.getOriginalFilename());
        map.put("size",file.getSize());

        return Result.success(map);
    }


}
